package com.guodi.model.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guodi.model.entity.ApiIdtIndicatorsDir;
import com.guodi.model.entity.IdtIndicators;
import com.guodi.model.entity.IdtIndicatorsType;
import com.guodi.model.entity.IdtTargetValue;
import com.guodi.model.service.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @描述: 指标目录远程调用实现
 * @入参:
 * @出参:
 * @作者: 余家鑫
 * @日期: 2021/4/21 上午10:18
 */
@ApiIgnore
@RestController

public class IdtIndicatorsDirClient implements IIdtIndicatorsDirClient {

    @Resource
    private IIdtIndicatorsTypeService iIdtIndicatorsTypeService;
    @Resource
    private IIdtIndicatorsDirService idtIndicatorsDirService;
    @Resource
    private IIdtYearRelevanceService idtYearRelevanceService;
    @Resource
    private IIdtIndicatorsService iIdtIndicatorsService;
    @Resource
    private IIdtTargetValueService iIdtTargetValueService;

    @Override
    public List<ApiIdtIndicatorsDir> treeDataByCode(String code, String areaCode, String year) {
        //获取根据编码获取体系类型
        LambdaQueryWrapper<IdtIndicatorsType> lambda = new QueryWrapper().lambda();
        lambda.eq(IdtIndicatorsType::getCode, code);
        IdtIndicatorsType one = iIdtIndicatorsTypeService.getOne(lambda);
        //获取父节点下的所有数据
        List<ApiIdtIndicatorsDir> treeList = idtIndicatorsDirService.listDirByCode(one.getId(), null, null);

        //idtIndicatorsDirService.formatApiIndicatorsValList(treeList, year, areaCode);
        //创建树形结构
        List<ApiIdtIndicatorsDir> tree = createTree(one.getId(), treeList);
        return treeList;
    }

    @Override
    public String getDirIdByTypeCode(String typeCode) {
        //获取根据编码获取体系类型
        LambdaQueryWrapper<IdtIndicatorsType> lambda = new QueryWrapper().lambda();
        lambda.eq(IdtIndicatorsType::getCode, typeCode);
        IdtIndicatorsType one = iIdtIndicatorsTypeService.getOne(lambda);
        if(Objects.nonNull(one) && StringUtils.isNotBlank(one.getId())){
            return one.getId();
        }
        return "";
    }

    @Override
    public List<ApiIdtIndicatorsDir> listAllById(String id, String areaCode, String year) {
        //获取父节点下的所有数据
        List<ApiIdtIndicatorsDir> treeList = idtIndicatorsDirService.listDirByCode(id, null, null);

        //idtIndicatorsDirService.formatApiIndicatorsValList(treeList, year, areaCode);
        //创建树形结构
        //List<ApiIdtIndicatorsDir> tree = createTree(id, treeList);
        return treeList;
    }

    @Override
    public Integer getPlanTargetYearByIndicatorId(String indicatorsId) {
        return idtYearRelevanceService.getPlanTargetYearByIndicatorId(indicatorsId);
    }

    @Override
    public Map<String, String> mapIndicatorsCode2Id(List<String> indicatorsCodes) {
        if(CollectionUtils.isEmpty(indicatorsCodes)){
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<IdtIndicators> condition = new QueryWrapper().lambda();
        condition.select(IdtIndicators::getCode, IdtIndicators::getId);
        condition.in(IdtIndicators::getCode, indicatorsCodes);
        List<IdtIndicators> list = iIdtIndicatorsService.list(condition);
        Map<String, String> collect = list.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e.getId(), (e1, e2) -> e2));
        return collect;
    }

    @Override
    public Map<String, IdtIndicators> mapIndicatorsId2Entity(List<String> indicatorsIds) {
        if(CollectionUtils.isEmpty(indicatorsIds)){
            return Collections.emptyMap();
        }

        LambdaQueryWrapper<IdtIndicators> condition = new QueryWrapper().lambda();
        condition.in(IdtIndicators::getId, indicatorsIds);
        List<IdtIndicators> list = iIdtIndicatorsService.list(condition);

        Map<String, IdtIndicators> collect = list.stream().collect(Collectors.toMap(e -> e.getId(), e -> e, (e1, e2) -> e2));
        return collect;
    }

    @Override
    public Map<String, IdtIndicators> mapCode2IdtIndicators() {
        List<IdtIndicators> list = iIdtIndicatorsService.list();
        Map<String, IdtIndicators> collect = list.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e, (e1, e2) -> e2));
        return collect;
    }

    @Override
    public List<IdtTargetValue> listIdtTargetValueByDivisionCode(String divisionCode) {
        LambdaQueryWrapper<IdtTargetValue> condition = new QueryWrapper().lambda();
        condition.eq(IdtTargetValue::getDivisionCode, divisionCode);
        List<IdtTargetValue> list = iIdtTargetValueService.list(condition);
        return list;
    }

    @Override
    public Map<String, String> mapIndicatorsId2Name(String[] indicatorsIds) {
        if(indicatorsIds == null || indicatorsIds.length <= 0){
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<IdtIndicators> condition = new QueryWrapper().lambda();
        condition.select(IdtIndicators::getId, IdtIndicators::getName);
        condition.in(IdtIndicators::getId, indicatorsIds);
        List<IdtIndicators> list = iIdtIndicatorsService.list(condition);
        Map<String, String> collect = list.stream().collect(Collectors.toMap(e -> e.getId(), e -> e.getName(), (e1, e2) -> e2));
        return collect;
    }

    @Override
    public Map<String, String> mapIndicatorsId2Unit(String[] indicatorsIds) {
        if(indicatorsIds == null || indicatorsIds.length <= 0){
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<IdtIndicators> condition = new QueryWrapper().lambda();
        condition.select(IdtIndicators::getId, IdtIndicators::getUnit);
        condition.in(IdtIndicators::getId, indicatorsIds);
        List<IdtIndicators> list = iIdtIndicatorsService.list(condition);
        Map<String, String> collect = list.stream().collect(Collectors.toMap(e -> e.getId(), e -> e.getUnit(), (e1, e2) -> e2));
        return collect;
    }

    @Override
    public Map<String, String> mapIndicatorsName2Code(String[] indicatorsNames) {
        if(indicatorsNames == null || indicatorsNames.length <= 0){
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<IdtIndicators> condition = new QueryWrapper().lambda();
        condition.select(IdtIndicators::getName, IdtIndicators::getCode);
        condition.in(IdtIndicators::getName, indicatorsNames);
        List<IdtIndicators> list = iIdtIndicatorsService.list(condition);
        Map<String, String> collect = list.stream().collect(Collectors.toMap(e -> e.getName(), e -> e.getCode(), (e1, e2) -> e2));
        return collect;
    }

    private List<ApiIdtIndicatorsDir> createTree(String parentId, List<ApiIdtIndicatorsDir> menus) {
        List<ApiIdtIndicatorsDir> tree = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++) {
            ApiIdtIndicatorsDir idtDirectory = menus.get(i);
            if (idtDirectory.getParentId().equals(parentId)) {
                tree.add(idtDirectory);
                List children = createTree(idtDirectory.getId(), menus);
                if (children != null && !children.isEmpty()) {
                    idtDirectory.setChildren(children);
                } else {
                    idtDirectory.setChildren(new ArrayList<>());
                }
            }
        }
        return tree;
    }
}
